home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995 February: Tool Chest / Dev.CD Feb 95 / Dev.CD Feb 95.toast / Tool Chest / Development Tools & Languages / Dylan Related / Mindy-1.1 (sources only) / mindy-1.1 / libraries / dylan / debug.dylan < prev    next >
Encoding:
Text File  |  1994-06-28  |  7.9 KB  |  309 lines  |  [TEXT/ttxt]

  1. module: dylan
  2. rcs-header: $Header: debug.dylan,v 1.7 94/06/27 17:10:21 wlott Exp $
  3.  
  4. //======================================================================
  5. //
  6. // Copyright (c) 1994  Carnegie Mellon University
  7. // All rights reserved.
  8. // 
  9. // Use and copying of this software and preparation of derivative
  10. // works based on this software are permitted, including commercial
  11. // use, provided that the following conditions are observed:
  12. // 
  13. // 1. This copyright notice must be retained in full on any copies
  14. //    and on appropriate parts of any derivative works.
  15. // 2. Documentation (paper or online) accompanying any system that
  16. //    incorporates this software, or any part of it, must acknowledge
  17. //    the contribution of the Gwydion Project at Carnegie Mellon
  18. //    University.
  19. // 
  20. // This software is made available "as is".  Neither the authors nor
  21. // Carnegie Mellon University make any warranty about the software,
  22. // its performance, or its conformity to any specification.
  23. // 
  24. // Bug reports, questions, comments, and suggestions should be sent by
  25. // E-mail to the Internet address "gwydion-bugs@cs.cmu.edu".
  26. //
  27. //======================================================================
  28. //
  29. // This file contains the support routines used by the debugger.
  30. //
  31.  
  32. define method report-problem (problem)
  33.   block ()
  34.     report-condition(problem);
  35.   exception <error>
  36.     puts("\nproblem reporting problem... giving up");
  37.   end;
  38. end;
  39.  
  40.  
  41. define constant debug-variables = make(<stretchy-vector>);
  42.  
  43.  
  44. define method debugger-flush ()
  45.   debug-variables.size := 0;
  46.   puts("Flushed all debugger variables.\n");
  47.   values();
  48. end;
  49.  
  50.  
  51.  
  52. define method eval-debugger-expr (expr, num-debug-vars)
  53.   select (head(expr))
  54.     debug-var: =>
  55.       let var = tail(expr);
  56.       block ()
  57.     if (var < 0)
  58.       debug-variables[num-debug-vars + var];
  59.     else
  60.       debug-variables[var];
  61.     end;
  62.       exception <error>
  63.     error("No debug variable $%=", var);
  64.       end;
  65.     literal: => tail(expr);
  66.     funcall: =>
  67.       apply(method (func, #rest args) apply(func, args) end,
  68.         map(rcurry(eval-debugger-expr, num-debug-vars), tail(expr)));
  69.   end;
  70. end method;
  71.  
  72.  
  73. define method debugger-eval (expr)
  74.   block ()
  75.     block ()
  76.       let (#rest results) = eval-debugger-expr(expr, debug-variables.size);
  77.       values(#t, results);
  78.     exception (problem :: <error>)
  79.       puts("invocation failed:\n  ");
  80.       report-problem(problem);
  81.       putc('\n');
  82.       #f;
  83.     end;
  84.   exception (<error>)
  85.     puts("Could not recover from earlier error.\n");
  86.     #f;
  87.   end;
  88. end;
  89.     
  90.  
  91. define method eval-and-print (expr, num-debug-vars)
  92.   let (#rest results) = eval-debugger-expr(expr, num-debug-vars);
  93.   if (empty?(results))
  94.     puts("[0 values returned]");
  95.   else
  96.     for (first = #t then #f,
  97.      result in results)
  98.       unless (first)
  99.     puts(", ");
  100.       end;
  101.       format("$%==%=", debug-variables.size, result);
  102.       add!(debug-variables, result);
  103.     end;
  104.   end;
  105.   putc('\n');
  106. end method;
  107.  
  108. define method debugger-call (exprs)
  109.   let num-debug-vars = debug-variables.size;
  110.   for (expr in exprs)
  111.     block ()
  112.       eval-and-print(expr, num-debug-vars);
  113.     exception (<abort>, init-arguments: list(description: "Blow off call"))
  114.       #f;
  115.     end;
  116.   end;
  117. end;
  118.  
  119. define method debugger-print (exprs)
  120.   block ()
  121.     let num-debug-vars = debug-variables.size;
  122.     for (expr in exprs)
  123.       block ()
  124.     eval-and-print(expr, num-debug-vars);
  125.       exception (problem :: <error>)
  126.     puts("invocation failed:\n  ");
  127.     report-problem(problem);
  128.     putc('\n');
  129.       end;
  130.     end;
  131.   exception (<error>)
  132.     puts("Could not recover from earlier error.\n");
  133.   end;
  134. end;
  135.  
  136. define method debugger-report-condition (cond)
  137.   block ()
  138.     putc('\n');
  139.     block ()
  140.       report-condition(cond);
  141.     exception (problem :: <error>)
  142.       puts("problem reporting condition:\n  ");
  143.       report-problem(problem);
  144.     end;
  145.     puts("\n\n");
  146.   exception <error>
  147.     puts("\nCould not recover from earlier errors.\n\n");
  148.   end;
  149. end;
  150.   
  151.  
  152. define method debugger-abort ()
  153.   block ()
  154.     block ()
  155.       signal(make(<abort>));
  156.     exception (problem :: <error>)
  157.       puts("problem signaling abort restart:\n  ");
  158.       report-problem(problem);
  159.       putc('\n');
  160.     end;
  161.   exception <error>
  162.     puts("Could not recover from earlier errors.\n");
  163.   end block;
  164. end;
  165.  
  166.  
  167. define method debugger-describe-restarts (cond)
  168.   block ()
  169.     block ()
  170.       let index = 0;
  171.       for (h = current-handler() then h.handler-next, while h)
  172.     let type = h.handler-type;
  173.     if (instance?(type, <class>) & subtype?(type, <restart>))
  174.       block ()
  175.         format("%= [%=]: ", index, type);
  176.         report-condition(apply(make, type, h.handler-init-args));
  177.       exception (problem :: <error>)
  178.         puts("\nproblem describing restart:\n  ");
  179.         report-problem(problem);
  180.       end;
  181.       putc('\n');
  182.       index := index + 1;
  183.     end if;
  184.       end for;
  185.       if (zero?(index))
  186.     puts("No active restarts.\n");
  187.       end;
  188.     exception (problem :: <error>)
  189.       puts("\nproblem describing restarts:\n  ");
  190.       report-problem(problem);
  191.       putc('\n');
  192.     end;
  193.     block ()
  194.       if (instance?(cond, <condition>) & return-allowed?(cond))
  195.     block ()
  196.       puts("\nReturning is allowed");
  197.       let description = return-description(cond);
  198.       select (description by instance?)
  199.         singleton(#f) =>
  200.           #f;
  201.         <byte-string> =>
  202.           puts(":\n  ");
  203.           puts(description);
  204.         <restart> =>
  205.           puts(":\n  ");
  206.           report-condition(description);
  207.       end;
  208.     exception (problem :: <error>)
  209.       puts("\nproblem describing return convention:\n  ");
  210.       report-problem(problem);
  211.     end block;
  212.     putc('\n');
  213.       end if;
  214.     exception (problem :: <error>)
  215.       puts("\nproblem checking on return contention:\n  ");
  216.       report-problem(problem);
  217.       putc('\n');
  218.     end block;
  219.   exception <error>
  220.     puts("\nCould not recover from earlier errors.\n");
  221.   end block;
  222. end method;
  223.  
  224. define method debugger-restart (cond, index)
  225.   block (return)
  226.     let count = 0;
  227.     for (h = current-handler() then h.handler-next, while h)
  228.       let type = h.handler-type;
  229.       let test = h.handler-test;
  230.       if (instance?(type, <class>) & subtype?(type, <restart>))
  231.     if (count == index)
  232.       block ()
  233.         let restart = apply(make, type, h.handler-init-args);
  234.         restart-query(restart);
  235.         unless (~test | test(h))
  236.           puts("The restart handler refused to handle the restart.\n");
  237.           return(#f);
  238.         end;
  239.         local
  240.           method next-handler ()
  241.         puts("The restart handler declined to handle the restart.\n");
  242.         return(#f);
  243.           end;
  244.         let (#rest values) = h.handler-function(restart, next-handler);
  245.         if (instance?(cond, <condition>) & return-allowed?(cond))
  246.           return(#t, values);
  247.         else
  248.           puts("The restart handler tried to return, but returning "
  249.              "is not allowed\n");
  250.           return(#f);
  251.         end if;
  252.       exception (problem :: <error>)
  253.         puts("Problem while attempting to restart:\n  ");
  254.         report-problem(problem);
  255.         putc('\n');
  256.         return(#f);
  257.       end block;
  258.     else
  259.       count := count + 1;
  260.     end if;
  261.       end if;
  262.     end for;
  263.     if (zero?(count))
  264.       puts("No active restarts.\n");
  265.     else
  266.       format("Invalid restart number, should be less than %d\n", count);
  267.     end if;
  268.     #f;
  269.   exception <error>
  270.     puts("Could not recover from earlier errors.\n");
  271.     #f;
  272.   end block;
  273. end method;
  274.       
  275.     
  276. define method debugger-return (cond)
  277.   block (return)
  278.     block ()
  279.       if (instance?(cond, <condition>) & return-allowed?(cond))
  280.     block ()
  281.       let (#rest values) = return-query(cond);
  282.       return(#t, values);
  283.     exception (problem :: <error>)
  284.       puts("problem quering for values to return:\n  ");
  285.       report-problem(problem);
  286.       putc('\n');
  287.       return(#f);
  288.     end;
  289.       else
  290.     puts("Returning is not allowed\n");
  291.     return(#f);
  292.       end;
  293.     exception (problem :: <error>)
  294.       puts("problem checking to see if returning is allowed:\n  ");
  295.       report-problem(problem);
  296.       putc('\n');
  297.       return(#f);
  298.     end;
  299.   exception <error>
  300.     puts("Could not recover from earlier errors.\n");
  301.     #f;
  302.   end block;
  303. end method;
  304.  
  305.  
  306.  
  307. // Now that we have the dylan helper routines defined, enable the error system.
  308. enable-error-system();
  309.